#ifndef ARRAYLISTWINDOW_H
#define ARRAYLISTWINDOW_H

#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGroupBox>
#include <QMessageBox>
#include <QScrollArea>
#include <QTimer>
#include <QDialog>
#include <QFormLayout>

// 输入对话框类
class InputDialog : public QDialog
{
    Q_OBJECT

public:
    // 两个参数的构造函数 - 用于删除和查找操作
    InputDialog(const QString &title, const QString &label, QWidget *parent = nullptr);
    // 三个参数的构造函数 - 用于插入操作
    InputDialog(const QString &title, const QString &label1, const QString &label2, QWidget *parent = nullptr);
    QString getFirstValue() const;
    QString getSecondValue() const;

private:
    void setupUI(const QString &title, const QString &label1, const QString &label2 = "");

    QLineEdit *m_firstEdit;
    QLineEdit *m_secondEdit;
};

class ArrayListWindow : public QMainWindow
{
    Q_OBJECT

public:
    ArrayListWindow(QWidget *parent = nullptr);
    ~ArrayListWindow();

signals:
    void returnToMainMenu();

private slots:
    void onInputData();
    void onInsert();
    void onRemove();
    void onSearch();
    void onClear();
    void onReturnToMainMenu();
    void onAnimationStep();

private:
    void setupUI();
    void updateDisplay();
    void visualizeDataStructure();
    void startInsertAnimation(int index, int value);
    void startRemoveAnimation(int value);
    void startSearchAnimation(int value);
    void updateVisualizationWithHighlight(int highlightIndex = -1, int newValueIndex = -1, int searchIndex = -1, int moveFromIndex = -1, int moveToIndex = -1);
    void resetData();
    void appendToInfo(const QString &text);
    void copyArray(const int* source, int* destination, int count);

    // 使用原生数组替代 QVector
    int* m_data;           // 动态分配的数组
    int* m_originalData;   // 动画备份数据
    int m_capacity;
    int m_size;

    // 动画相关变量
    QTimer *m_animationTimer;
    int m_animationStep;
    int m_animationIndex;
    int m_animationValue;
    int m_originalSize;
    QString m_animationType; // "insert", "remove", "search"
    int m_moveFromIndex; // 当前移动的起始位置
    int m_moveToIndex;   // 当前移动的目标位置

    QTextEdit *m_display;
    QLineEdit *m_inputEdit;

    QWidget *m_visualizationWidget;
    QScrollArea *m_scrollArea;
};

#endif // ARRAYLISTWINDOW_H
